package com.ian.offer;

import java.util.Stack;

/**
 * @author zhangyaowen
 * @date 2021/4/20 10:32 下午
 */
public class ValidateStackSequences {

    private Stack stack = new Stack();
    private int p = 0;

    public boolean validateStackSequences(int[] pushed, int[] popped) {

        for (int i = 0; i < pushed.length; i++) {
            stack.push(pushed[i]);
            checkpop(popped);
        }
        return stack.isEmpty();
    }

    private void checkpop(int[] popped) {
        while(!stack.isEmpty()){
            if ((int)stack.peek() == popped[p]){
                stack.pop();
                p++;
            }else{
                break;
            }
        }
    }


    public static void main(String[] args) {
        ValidateStackSequences validateStackSequences = new ValidateStackSequences();
        int[] a = {1,2,3,4,5};
        int[] b = {4,5,3,1,2};
        boolean flag =validateStackSequences.validateStackSequences(a,b);
        System.out.println(flag);
    }


}
